package dfbz.homework1;

import java.util.ArrayList;

public class Test07 {

    public static void main(String[] args) {
        Worker w1 = new Worker("bz001", "黄渤", 45);
        Worker w2 = new Worker("bz002", "孙红雷", 48);
        Worker w3 = new Worker("bz003", "罗志祥", 35);
        ArrayList<Worker> list = new ArrayList<>();
        list.add(w1);
        list.add(w2);
        list.add(w3);
        Worker maxAge = findMaxAge(list);
        maxAge.show();

        //5. 定义findMaxAge方法,参数列表(ArrayList workers), 返回值类型void, findMaxAge方法的功能是获取参
        //数workers集合中最大年龄的Worker对象,并调用大年龄对象的show方法
        //6. 在findMaxAge方法中定义Worker类型的变量maxAgeWorker,maxAgeWorker变量保存最大年龄的
        //Worker,初始化值为workers集合索引为0的对象
        //7. 在findMaxAge方法中使用for循环遍历workers集合中的每个worker对象
        //8. 如果取出的worker对象年龄大于maxAgeWorker的年龄,修改maxAgeWorker值为取出的worker对象
        //9. 调用maxAgeWorker对象的show方法
        //10. 在main方法中调用findMaxAge方法,参数传入上面创建的Arraylist集
    }

    public static Worker findMaxAge(ArrayList<Worker> workers) {
        int maxAgeWorker = workers.get(0).getAge();
        Worker worker = null;
        for (int i = 0; i < workers.size(); i++) {
            if (maxAgeWorker < workers.get(i).getAge()) {
                maxAgeWorker = workers.get(i).getAge();
                worker = workers.get(i);
            }
        }
        return worker;
    }
}
